clear all


global dir "."	
global data $dir/data
global out $dir/output
local figbacks "plotregion(fcolor(white)) graphregion(fcolor(white) lwidth(large)) bgcolor(white)"
local theusual "nomtitles nonotes noobs nonumbers nolines nogaps label prehead(" ") posthead(" ") prefoot(" ") postfoot(" ")"
set scheme s1color
	
************************************************
*Table 2 Summary Statistics
************************************************

use $data/cedapanel,clear
egen tagflag=tag(geocode)
preserve
foreach g in 1 2 3 4{
	keep if sp_typ==`g'
	gen ppop_=pop_/1000
	eststo t0_`g': estpost tabstat tagflag if tagflag==1, s(sum) col(stat)
	eststo t1_`g': estpost tabstat ppop_ if tagflag==1, s(mean sd) col(stat)
	keep if YEAR<2001
	gen not_tygo=count-ty_go
	gen pa_nottygo=pa_count-pa_ty_go
	gen dps=dollarsd/pop_relev
	gen pdps=pa_dollars/pop_relev
	replace dollars=dollars/1000000
	replace pa_dollars=pa_dollars/1000000

	collapse(sum) count ty_go not_tygo pa_nottygo pa_count pa_ty_go (mean)  dollarsd pa_dollars dps pdps, by(geocode)
	gen d_ty=ty_go>0
	gen d_nty=not_tygo>0
	gen d_pa_ty=pa_ty>0
	gen d_pa_nty=pa_notty>0
	eststo t2_`g': estpost tabstat d_ty d_pa_ty d_nty d_pa_nty dollarsd dps pa_doll pdps, s(mean sd) col(stat)
	restore, preserve
}

label var tagflag "Count"
gen ppop_=.
label var ppo "Population (1,000)"
label var pctnh "Non-Hispanic White"
label var pctblack "African American"
label var pctasia "Asian"
label var pcthisp "Hispanic"

gen d_ty=.
	label var d_ty "Proposed GO Bond"
gen d_nty=. 
	label var d_nty "Proposed Other Funding Election"
gen d_pa_ty=.
	label var d_pa_ty "Passed GO Bond"
gen d_pa_nty=.
	label var d_pa_nty "Passed Other Funding Election"
label var dollarsd "Proposed Bond Size (\$million)"
gen dps=.
	label var dps "Proposed Bond Size (\$ per Resident)"
label var pa_dollars "Passed Bond Size (\$million)"
gen pdps=.
	label var pdps "Passed Bond Size (\$ per Resident)"

esttab t0_1 t0_3 t0_2 t0_4 using "$out/Table2_t0.tex", main(sum) `theusual' replace
esttab t1_1 t1_3 t1_2 t1_4 using "$out/Table2_t1.tex", main(mean) `theusual' replace
esttab t2_1 t2_3 t2_2 t2_4 using "$out/Table2_t2.tex", main(mean) `theusual' replace
restore
************************************************
*** Tax/Bond use over Time
************************************************

***Figure 1 Panel B, Figure A2, and Figure A3
*Separately by Ed and Non-Ed

use $data/cedapanel,clear
collapse (sum) d_ty_go d_pa_ty_go dollars pa_dollars, by(YEAR treat)
replace dollars=dollars/1000000
replace pa_dollars=pa_dollars/1000000
forvalue ed=0/1 {
	preserve
		keep if treat==`ed'
		#delimit ;
		scatter d_* YEAR, connect(l l) 
			mcolor(black black) msymbol(o oh) lcolor(black black) lpattern(solid dash) 
			xline(2001, lcolor(gray))
			xlabel(1995(2)2016) 
			legend(order(1 "Proposed GO Bonds" 2 "Passed GO Bonds"))
			xtitle(Year);
		#delimit cr
		graph export "$out/saved/ceda_trend_ed`ed'.png", replace
		#delimit ;
		scatter dollars pa_ YEAR, connect(l l) 
			mcolor(black black) msymbol(o oh) lcolor(black black) lpattern(solid dash) 
			xline(2001, lcolor(gray))
			xlabel(1995(2)2016) 
			legend(order(1 "Proposed GO Bonds" 2 "Passed GO Bonds"))
			xtitle(Year);
		#delimit cr
		graph export "$out/saved/ceda_trend_dol_ed`ed'.png", replace		
	restore
}
copy "$out/saved/ceda_trend_ed1.png" "$out/Figure1_PanelB.png"
copy "$out/saved/ceda_trend_ed0.png" "$out/FigureA2.png"
copy "$out/saved/ceda_trend_dol_ed1.png" "$out/FigureA3_PanelA.png"
copy "$out/saved/ceda_trend_dol_ed0.png" "$out/FigureA3_PanelB.png"

***Figure A13
*Property Taxes etc
use $data/cedapanel,clear
collapse (sum) d_ty_pt d_pa_ty_pt, by(YEAR treat)

local ed=1
preserve
	keep if treat==`ed'
	#delimit ;
	scatter d_* YEAR, connect(l l) 
		mcolor(black black) msymbol(o oh) lcolor(black black) lpattern(solid dash) 
		xline(2001, lcolor(gray))
		xlabel(1995(2)2016) 
		legend(order(1 "Proposed Prop Tax" 2 "Passed Prop Tax"))
		xtitle(Year);
	#delimit cr
	graph export "$out/FigureA13.png", replace
restore



********************
*Figure A14
*********************
use $data/censusfinance, clear
forvalues y=1996/2013{
	gen yy`y'=year==`y'
}
*rename yy2001 omit

foreach v of varlist *PerStu{
	reg `v' yy* if year>1994, absorb(NCESID) cluster(NCESID)
	mat z`v'=1995, 0, 0
	forvalues y=1996/2013{
		*if `y'!=2001{
			mat a=`y', _b[yy`y'], _se[yy`y']
			mat z`v'=z`v'\a
		*}
	}
}


	mat zz=zcurexpPerStu\ zexpPerStu
	clear
	svmat zz
	rename(*)(y b1 s1)
		foreach g in 1 {
		gen up`g'=b`g'+1.96*s`g'
		gen do`g'=b`g'-1.96*s`g'
		}
		gen cur=_n<20
		replace y=y+0.2 if cur==1
	
			
		twoway(scatter b1 y if cur==1, mcolor(gray) msymbol(Oh)) ///
				(rcap up1 do1 y if cur==1, lcolor(gray)) ///
				(scatter b1 y if cur==0, mcolor(black)) ///
				(rcap up1 do1 y if cur==0, lcolor(black)), ///
				xline(2001.9) ///
				yline(0, lcolor(gray)) ytitle("Expenditures per Student") ///
				xtitle(Year) legend(order(3 "Total Expenditures" 1 "Current Expenditures"))
				graph export "$out/FigureA14.png", replace

	




***********************************************************
*UNCONDITIONAL JURIS-LEVEL REGS*
**********************************************************

use $data/cedapanel,clear

label var post "Post 2001"
label var post_treat "Post x Treat"
label var treat "Treated "
gen oddy=mod(YEAR,2)
replace dollars=dollars/(pop_relev)
replace pa_dollars=pa_dollars/(pop_relev)
levelsof county, local(geolo)
	foreach g of local geolo{
		gen trendgeo_`g'=(county==`g')*(YEAR)
		}
qui tab YEAR, gen(yearfe)


sum pctnhwhite if YEAR==2000,d
	local mednhwhite=r(p50)
	gen highminority=(pctnhwhite<`mednhwhite')
	gen post_highminority=highminority*post
	gen treatpost_highminority=highminority*treat*post
gen pctalloth=1-pctblack-pctnh-pcthi-pctas
gen fractional=1-(pctblack^2)-(pctnhwhite^2)-(pcthisp^2)-(pctasian^2)-(pctalloth^2)
	sum fractional if YEAR==2000, d
	local medfrac=r(p50)
	gen highfrac=(fractional>`medfrac')
	gen post_highfrac=highfrac*post
	gen treatpost_highfrac=highfrac*treat*post

gen logtranspa_dollars=log(pa_dollars+1)
gen logtransdollars=log(dollars+1)
gen ihspa_dollars=ln(pa_dollar + ((pa_dollar^2 +1)^0.5))
gen ihsdollars=ln(dollars + ((dollars^2 +1)^0.5))

gen midterm=mod(YEAR,2)==0
gen president=mod(YEAR,4)==0
	replace midterm=0 if president==1


*DD Regressions

preserve
eststo clear
foreach out in  d_pa_ty_ptax d_ty_ptax d_count count d_pa_count pa_count d_ty_go d_pa_ty_go dollars pa_dollars {

	eststo `out'4: reghdfe `out'       post post_treat, absorb(YEAR geo) cluster(geo)
				estadd ysumm
				estimates save $out/saved/ceda_disle_dd_`out'_4, replace
	eststo z`out'4a: reghdfe `out'       post post_treat if YEAR<2007, absorb(countycode##YEAR geo) cluster(geo)
				estadd ysumm
				estimates save $out/saved/ceda_disle_dd_`out'_4a, replace					
	eststo z`out'5: reghdfe `out'       post post_treat if sp_type!=3, absorb(YEAR geo) cluster(geo)
				estadd ysumm
				estimates save $out/saved/ceda_disle_dd_`out'_5, replace			
	eststo z`out'7: reghdfe `out' post_treat, absorb(countycode##YEAR geo) cluster(geo)
				estadd ysumm
				estimates save $out/saved/ceda_disle_dd_`out'_7, replace
	eststo z`out'7w: reghdfe `out' post_treat [aweight=pop_relev], absorb(countycode##YEAR geo) cluster(geo)
				estadd ysumm
				estimates save $out/saved/ceda_disle_dd_`out'_7w, replace
	eststo z`out'7oc: reghdfe `out' post_treat if overcrowdedN==0 | type != "k12", absorb(countycode##YEAR geo) cluster(geo)
				estadd ysumm
				estimates save $out/saved/ceda_disle_dd_`out'_7oc, replace
	eststo `out'4b: reghdfe `out'       post post_treat president midterm, absorb( geo) cluster(geo)
				estadd ysumm
				estimates save $out/saved/ceda_disle_dd_`out'_4b, replace
	eststo z`out'9: reghdfe `out' post_highfrac treatpost_highfrac post_treat, absorb(YEAR geo) cluster(geo)
		estadd ysumm
		estimates save $out/saved/ceda_disle_dd_`out'_9, replace			
	eststo z`out'10: reghdfe `out' post_highfrac treatpost_highfrac post_treat , absorb(countycode##YEAR geo) cluster(geo)
		estadd ysumm
		estimates save $out/saved/ceda_disle_dd_`out'_10, replace			
	eststo z`out'8oc: reghdfe `out' post_highminority treatpost_highminority post_treat if (overcrowdedN==0 | type !="k12"), absorb(countycode##YEAR geo) cluster(geo)
		estadd ysumm
		estimates save $out/saved/ceda_disle_dd_`out'_8oc, replace			
	eststo z`out'10oc: reghdfe `out' post_highfrac treatpost_highfrac post_treat if (overcrowdedN==0 | type !="k12"), absorb(countycode##YEAR geo) cluster(geo)
		estadd ysumm
		estimates save $out/saved/ceda_disle_dd_`out'_10oc, replace				


}
restore

*************************************
*Pictures
*************************************
*effect by quantile of 1-pctnhwhite

merge m:1 idcounty using $data/cens2000pov_county
	drop _merge
merge m:1 idcity using $data/cens2000pov_city
	drop _merge
merge m:1 idk12 using $data/cens2000pov_k12
	drop _merge
merge m:1 idcc using $data/cens2000pov_cc
	drop _merge
	
foreach var in pov age65{
gen `var'2000=.
	foreach g in cou  cc cit{
		replace `var'2000=`var'2000`g'/pop_relev if `var'2000`g'!=.
		}
	replace `var'2000=`var'2000k12 if `var'2000k12!=.
	}
gen xvar_appro=pa_dollars if YEAR<2001
	bysort geo: egen mmm=mean(xvar_appro)
	replace xvar_appro=mmm
gen xvar_minority=1-pctnhwhite

gen xvar_pop=ln(pop_relev)
gen xvar_pov=pov2000
gen xvar_age65=age652000
foreach g in nhwhite black asian hisp{
	gen xvar_`g'=pct`g'
}
replace xvar_nhwhite=1-xvar_minority
cap gen pctalloth=1-pctblack-pctnh-pcthi-pctas
gen xvar_fractional=1-(pctblack^2)-(pctnhwhite^2)-(pcthisp^2)-(pctasian^2)-(pctalloth^2)
local range_minority = "0 1"
local range_pop="6 13"
local range_pov="0 0.4"
local range_age65="0 0.3"
local range_appro="0 100"
local range_fractional="0 1"
local range_hisp= "0 1"
local range_black = "0 0.2"
local range_asian = "0 0.3"
local range_nhwhite = "0 1"
local xtit_minority "Percent Minority"
local xtit_pop "Log Population"
local xtit_pov "Poverty Rate"
local xtit_age65 "Age 65 or Older"
local xtit_appro "Bond Funding ($/Resident)"
local xtit_nhwhite "Percent White"
local xtit_black "Percent Black"
local xtit_asian "Percent Asian"
local xtit_hisp "Percent Latino"
local xtit_fractional "Fractionalization"
gen popthou=pop_relev/1000
*/

***Figure A12: Correlation Between Jurisdiction Non-White Share, Poverty Rate, and Population
binscatter xvar_pov xvar_min, line(none) xscale(r(0(0.1)1)) yscale(r(0(0.1).4)) xtitle("Percent Minority") ytitle("Poverty Rate") ylabel(0(0.1)0.4) `figbacks'
		graph export "$out/FigureA12_PanelA.png", replace
binscatter popthou  xvar_min, line(none) xscale(r(0(0.1)1)) yscale(r(0(100)300)) xtitle("Percent Minority") ytitle("Population (1000s)") `figbacks'
		graph export "$out/FigureA12_PanelB.png", replace


***Figure 5 & Figures A5-A11
foreach FE in   "i.YEAR" "i.countycode##i.YEAR"{
	local fileext ""
	if "`FE'"=="i.countycode##i.YEAR" {
		local fileext "_FE"
	}
	local nquantiles=5
	foreach g in nhwhite black asian hisp {
		xtile `g'quantile=pct`g', nquantiles(`nquantiles')
	}
	xtile fractionalquantile=xvar_frac, nquantiles(`nquantiles')
	xtile popquantile=pop_relev, nquantiles(`nquantiles')
	xtile povquantile=pov2000, nquantiles(`nquantiles')
	xtile age65quantile=age652000, nquantiles(`nquantiles')
	local znq=`nquantiles'-1
	xtile approquantile=xvar_appro if xvar_appro!=0, nquantiles(`znq')
	replace approq=approq+1
	replace approq=1 if xvar_appro==0
	forvalues quant=2/`nquantiles' {
		foreach var in pop pov age65 appro black hisp asian nhwhite fractional {
			gen post_`var'q`quant'=post*(`var'quantile==`quant')
			gen posttreat_`var'q`quant'=post*treat*(`var'quantile==`quant')
		}
	}
	
	
	foreach var in nhwhite  fractional  black  asian hisp  appro  pop pov age65 {
		foreach out in pa_dollars  d_pa_ty_go  {
			reghdfe `out' post_treat post_`var'q* posttreat_`var'q* , absorb(`FE' geo) cluster(geo)
			mat mat_`out'_q=J(`nquantiles',4,.) 
			*matrix format (b, se, pctminority, mean of out)
			mat mat_`out'_q[1,1]=_b[post_treat]
			mat mat_`out'_q[1,2]=_se[post_treat]
			qui sum xvar_`var' if `var'quantile==1
			mat mat_`out'_q[1,3]=r(mean)
			qui sum `out' if `var'quantile==1  & YEAR<2001
			mat mat_`out'_q[1,4]=r(mean)
			forvalues i=2/`nquantiles' {
				lincom post_treat+posttreat_`var'q`i'
				mat mat_`out'_q[`i',1]=r(estimate)
				mat mat_`out'_q[`i',2]=r(se)
				qui sum xvar_`var' if `var'quantile==`i'
				mat mat_`out'_q[`i',3]=r(mean)
				qui sum `out' if `var'quantile==`i' & YEAR<2001
				mat mat_`out'_q[`i',4]=r(mean)
			}
		
			preserve
				clear
				svmat mat_`out'_q
					rename mat_`out'_q1 b
					rename mat_`out'_q2 se
					rename mat_`out'_q3 t
					rename mat_`out'_q4 mea
					gen ul=b+1.96*se
					gen ll=b-1.96*se
					sort t
					#delimit;
					twoway (scatter b t, connect(l) lpattern(solid) lcolor(black) mcolor(black) msymbol(o))|| 
							(line ul ll t, lcolor(black black) lpattern(dash dash)), xscale(range(`range_`var''))
							legend(off) `figbacks' scale(1.96) xtitle("`xtit_`var''") ytitle("Treatment Effect");
						graph export "$out/saved/ceda_het`var'_`out'_q`nquantiles'`fileext'.png", replace;
					twoway (scatter mea t, mcolor(black) msymbol(O)),
							xscale(range(`range_`var''))
							legend(off) `figbacks' scale(1.96) xtitle("`xtit_`var''") ytitle("Mean of Outcome");
						graph export "$out/saved/ceda_het`var'_`out'_q`nquantiles'`fileext'_m.png", replace;
						#delimit cr

			restore

		}
	
		cap drop `var'quantile 
		cap drop post_`var'q* 
		cap drop posttreat_`var'q*
		mat drop _all
	}
}


local out "pa_dollars"
local m ""
local fig "5"
copy "$out/saved/ceda_hetfractional_`out'_q5`m'.png" "$out/Figure`fig'_PanelA.png"
copy "$out/saved/ceda_hetnhwhite_`out'_q5`m'.png" "$out/Figure`fig'_PanelB.png"
copy "$out/saved/ceda_hetpov_`out'_q5`m'.png" "$out/Figure`fig'_PanelC.png"
copy "$out/saved/ceda_hetage65_`out'_q5`m'.png" "$out/Figure`fig'_PanelD.png"
copy "$out/saved/ceda_hetappro_`out'_q5`m'.png" "$out/Figure`fig'_PanelE.png"

copy "$out/saved/ceda_hetfractional_d_pa_ty_go_q5_FE.png" "$out/FigureA5_PanelA.png"
copy "$out/saved/ceda_hetfractional_pa_dollars_q5_FE.png" "$out/FigureA5_PanelB.png"

local out "d_pa_ty_go"
local m ""
local fig "A6"
copy "$out/saved/ceda_hetfractional_`out'_q5`m'.png" "$out/Figure`fig'_PanelA.png"
copy "$out/saved/ceda_hetnhwhite_`out'_q5`m'.png" "$out/Figure`fig'_PanelB.png"
copy "$out/saved/ceda_hetpov_`out'_q5`m'.png" "$out/Figure`fig'_PanelC.png"
copy "$out/saved/ceda_hetage65_`out'_q5`m'.png" "$out/Figure`fig'_PanelD.png"
copy "$out/saved/ceda_hetappro_`out'_q5`m'.png" "$out/Figure`fig'_PanelE.png"

local out "pa_dollars"
local m "_m"
local fig "A7"
copy "$out/saved/ceda_hetfractional_`out'_q5`m'.png" "$out/Figure`fig'_PanelA.png"
copy "$out/saved/ceda_hetnhwhite_`out'_q5`m'.png" "$out/Figure`fig'_PanelB.png"
copy "$out/saved/ceda_hetpov_`out'_q5`m'.png" "$out/Figure`fig'_PanelC.png"
copy "$out/saved/ceda_hetage65_`out'_q5`m'.png" "$out/Figure`fig'_PanelD.png"
copy "$out/saved/ceda_hetappro_`out'_q5`m'.png" "$out/Figure`fig'_PanelE.png"

local fig=8
foreach g in hisp black asian pop {
	copy "$out/saved/ceda_het`g'_d_pa_ty_go_q5.png" "$out/FigureA`fig'_PanelA.png"
	copy "$out/saved/ceda_het`g'_pa_dollars_q5.png" "$out/FigureA`fig'_PanelB.png"
	copy "$out/saved/ceda_het`g'_d_pa_ty_go_q5_m.png" "$out/FigureA`fig'_PanelC.png"
	copy "$out/saved/ceda_het`g'_pa_dollars_q5_m.png" "$out/FigureA`fig'_PanelD.png"
	local fig=`fig'+1
}

***Figure A15  Heterogeneous Effects of Proposition 39 on Dollars Passed Per Resident, Excluding Critically Overcrowded Schools
local nquantiles=5
foreach g in nhwhite {
	xtile `g'quantile=pct`g', nquantiles(`nquantiles')
}
xtile fractionalquantile=xvar_frac, nquantiles(`nquantiles')
local znq=`nquantiles'-1
forvalues quant=2/`nquantiles' {
	foreach var in nhwhite fractional {
		gen post_`var'q`quant'=post*(`var'quantile==`quant')
		gen posttreat_`var'q`quant'=post*treat*(`var'quantile==`quant')
	}
}
	
foreach var in nhwhite fractional {
	foreach out in pa_dollars { 
		reghdfe `out' post_treat post_`var'q* posttreat_`var'q* if (overcrowdedN==0 | type !="k12"), absorb(i.YEAR geo) cluster(geo)
		mat mat_`out'_q=J(`nquantiles',4,.) 
		*matrix format (b, se, pctminority, mean of out)
		mat mat_`out'_q[1,1]=_b[post_treat]
		mat mat_`out'_q[1,2]=_se[post_treat]
		qui sum xvar_`var' if `var'quantile==1
		mat mat_`out'_q[1,3]=r(mean)
		qui sum `out' if `var'quantile==1  & YEAR<2001 & (overcrowdedN==0 | type !="k12")
		mat mat_`out'_q[1,4]=r(mean)
		forvalues i=2/`nquantiles' {
			lincom post_treat+posttreat_`var'q`i'
			mat mat_`out'_q[`i',1]=r(estimate)
			mat mat_`out'_q[`i',2]=r(se)
			qui sum xvar_`var' if `var'quantile==`i'
			mat mat_`out'_q[`i',3]=r(mean)
			qui sum `out' if `var'quantile==`i' & YEAR<2001 & (overcrowdedN==0 | type !="k12")
			mat mat_`out'_q[`i',4]=r(mean)
		}
		
		preserve
			clear
			svmat mat_`out'_q
				rename mat_`out'_q1 b
				rename mat_`out'_q2 se
				rename mat_`out'_q3 t
				rename mat_`out'_q4 mea
				gen ul=b+1.96*se
				gen ll=b-1.96*se
				sort t
				#delimit;
				twoway (scatter b t, connect(l) lpattern(solid) lcolor(black) mcolor(black) msymbol(o))|| 
						(line ul ll t, lcolor(black black) lpattern(dash dash)), xscale(range(`range_`var''))
						legend(off) `figbacks' scale(1.96) xtitle("`xtit_`var''") ytitle("Treatment Effect");
					graph export "$out/saved/ceda_hetnooc_`var'_`out'_q`nquantiles'.png", replace;
				twoway (scatter mea t, mcolor(black) msymbol(O)),
						xscale(range(`range_`var''))
						legend(off) `figbacks' scale(1.96) xtitle("`xtit_`var''") ytitle("Mean of Outcome");
					graph export "$out/saved/ceda_hetnooc_`var'_`out'_q`nquantiles'_m.png", replace;
					#delimit cr

				restore

	}
	
	cap drop `var'quantile 
	cap drop post_`var'q* 
	cap drop posttreat_`var'q*
	mat drop _all
}

copy "$out/saved/ceda_hetnooc_fractional_pa_dollars_q5.png" "$out/FigureA15_PanelA.png"
copy "$out/saved/ceda_hetnooc_nhwhite_pa_dollars_q5.png" "$out/FigureA15_PanelB.png"
copy "$out/saved/ceda_hetnooc_fractional_pa_dollars_q5_m.png" "$out/FigureA15_PanelC.png"
copy "$out/saved/ceda_hetnooc_nhwhite_pa_dollars_q5_m.png" "$out/FigureA15_PanelD.png"



*** Figure 2 & Figure 4
forvalues y=1995/2016{
	gen t_`y'=treat*(YEAR==`y')
}
rename t_2000 omityear

	*run them
foreach out in d_ty_go d_pa_ty_go dollars pa_dollars {
	reghdfe `out' yearfe* treat t_* , cluster(geo) absorb(county#YEAR)
	mat mat_`out'1=0,0,0
	forvalues g=1995/2016{
		if `g'!=2000{
		mat a=_b[t_`g'], _se[t_`g'], `g'
		mat mat_`out'1=mat_`out'1\a
		}			
	}
}
			
*spit them out
foreach reg in 1 {
	foreach out in  d_ty_go d_pa_ty_go dollars pa_dollars {
	preserve
		clear
		svmat mat_`out'`reg'
		rename mat_`out'`reg'1 b
		rename mat_`out'`reg'2 se
		rename mat_`out'`reg'3 t
		gen ul=b+1.96*se
		gen ll=b-1.96*se
		replace t=2000 if t==0
		gen t2=2*(ceil(t/2))
		bysort t2: egen b2=mean(b)
		replace b2=. if t2==t
		sort t
		#delimit;
		twoway (scatter b b2 t, connect(l l) lpattern(solid) lcolor(black red) mcolor(black none) msymbol(o none))|| 
				(line ul ll t if t<2000, lcolor(black black) lpattern(dash dash))||
				(line ul ll t if t>2000, lcolor(black black) lpattern(dash dash)),
				legend(off) `figbacks' scale(1.96) xtitle("Year") yline(0, lcolor(black)) xline(2000, lcolor(gray));
		#delimit cr
		graph export "$out/saved/ceda_matc_`out'_`reg'.png", replace
	restore
	}
}
copy "$out/saved/ceda_matc_d_ty_go_1.png" "$out/Figure2_PanelA.png"
copy "$out/saved/ceda_matc_dollars_1.png" "$out/Figure2_PanelB.png"
copy "$out/saved/ceda_matc_d_pa_ty_go_1.png" "$out/Figure4_PanelA.png"
copy "$out/saved/ceda_matc_pa_dollars_1.png" "$out/Figure4_PanelB.png"			

***Figure A16: Pre-trend estimates using alternative specifications
foreach out in   logtransdollars logtranspa_dollars ihspa_dollars ihsdollars {
	reghdfe `out' treat t_* , cluster(geo) absorb(county##YEAR)
	mat mat_`out'=0,0,0
	forvalues g=1995/2000{
		if `g'!=2000{
			mat a=_b[t_`g'], _se[t_`g'], `g'
			mat mat_`out'=mat_`out'\a
		}			
	}
	mat list mat_`out'
}

	*spit them out
// 			foreach out in  dollars pa_dollars logtransdollars logtranspa_dollars ihspa_dollars ihsdollars {
foreach pa in pa_ "" {
	clear
	foreach out in  logtrans`pa'dollars ihs`pa'dollars {
		svmat mat_`out'
		gen `out'_ul=mat_`out'1+1.96*mat_`out'2
		gen `out'_ll=mat_`out'1-1.96*mat_`out'2
	}
	rename mat_ihs`pa'dollars3 t
	replace t=2000 if t==0

	sort t

	twoway (scatter mat_*1 t, connect(l l) lpattern(solid) lcolor(green blue) mcolor(green blue) msymbol(o o)) (line *_ul t if t<2000, lcolor(green blue) lpattern(dash dash)) (line *_ll t if t<2000, lcolor(green blue) lpattern(dash dash)), legend(order(1 "Log(x+1)" 2 "IHS") rows(1)) `figbacks' scale(1.96) xtitle("Year") yline(0, lcolor(black))
	graph export "$out/saved/ceda_`pa'logpretrend.png", replace
}
copy "$out/saved/ceda_logpretrend.png" "$out/FigureA16_PanelA.png"
copy "$out/saved/ceda_pa_logpretrend.png" "$out/FigureA16_PanelB.png"			





*********************************************************
*Conditional Election-level regs
***********************************************************

use $data/cedarefs	, clear
cap drop _merge


label var post "Post 2001"
label var post_treat "Post x Treat"
label var treat "Treated "
gen oddy=mod(YEAR,2)
replace dollarsd=dollarsd/(pop_relev)
replace pa_dollars=pa_dollars/(pop_relev)
qui tab YEAR, gen(yearfe)
gen sh=PERCENT/100

gen all=1

qui tab RECTYPE, gen(recfe)



gen logdollarsd=log(dollarsd)

gen midterm=mod(YEAR,2)==0
gen president=mod(YEAR,4)==0
	replace midterm=0 if president==1
gen november=month(DATE)==11

***Figure 1: Panel A.
gen pctt=PERCENT/100
#delimit;
 twoway (kdensity pctt if YEAR<2001 & YEAR>1995 & treat==1,  lpattern(solid) lwidth(thick) lcolor(black))||
		(kdensity pctt if YEAR>2001 & YEAR<2017 & treat==1,  lpattern(dash) lwidth(thick)lcolor(black)), 
		legend(order(1 "1996-2000" 2 "2002-2016" )) 
		xline(.66666, lpattern(solid) lcolor(black)) xline(.55, lpattern(dash) lcolor(black)) 
		xsc(r(0(.10)1)) xlabel(0(.10)1) ytitle(Density) xtitle("Fraction of votes in favor");
		graph export "$out/Figure1_PanelA.png", replace;
#delimit cr

***Figure A4: Vote Share for GO Bonds, 1995-2016

preserve
keep if ty_go==1
collapse(mean) PERCENT pass, by(YEAR treat)
#delimit;
twoway (scatter PERCENT  YEAR if treat==1, connect(l) 	mcolor(black ) msymbol(o ) lcolor(black) lpattern(solid ) )(scatter PERCENT  YEAR if treat==0, 	mcolor(black ) msymbol(oh ) lcolor(black) lpattern(dash ) connect(l)),
	xline(2001, lcolor(gray))
	xlabel(1995(2)2016) ytitle("Vote Share") ylabel(00(10)100) 
	legend(order(1 "Treated Jurisdictions" 2 "Control Jurisdictions"))
	xtitle(Year);
#delimit cr	
graph export "$out/FigureA4.png", replace
restore	


replace treat=0 if ty_go==0
replace post_treat=0 if ty_go==0

*
foreach out in sh pass dollarsd pa_dollars logdollarsd {
	foreach z in ty_go {
		 eststo `out'`z'1: reghdfe `out' post treat post_treat  , absorb(YEAR) cluster(geo)
		 	estadd ysumm
		 	estimates save $out/saved/ceda_tax_dd_`out'_0a, replace
		 eststo `out'`z'1: reghdfe `out' post treat post_treat  , absorb(YEAR sp_type) cluster(geo)
		 	estadd ysumm
		 	estimates save $out/saved/ceda_tax_dd_`out'_0b, replace
		eststo `out'`z'1: reghdfe `out' post treat post_treat  if `z'==1, absorb(YEAR) cluster(geo)
		 	estadd ysumm
		 	estimates save $out/saved/ceda_tax_dd_`out'_1, replace
		//add gov type FE	
		eststo `out'`z'2: reghdfe `out' post treat post_treat if `z'==1, absorb(YEAR sp_type) cluster(geo)
			estadd ysumm
			estimates save $out/saved/ceda_tax_dd_`out'_2, replace
		//election timing instead
		eststo `out'`z'3a: reghdfe `out' post treat post_treat president midterm if `z'==1, absorb( sp_type) cluster(geo)
			estadd ysumm
			estimates save $out/saved/ceda_tax_dd_`out'_3a, replace
		//november vs. not
		eststo `out'`z'3b: reghdfe `out' post treat post_treat november if `z'==1, absorb(YEAR  sp_type) cluster(geo)
			estadd ysumm
			estimates save $out/saved/ceda_tax_dd_`out'_3b, replace
							
// 		pre07
		eststo `out'`z'4: reghdfe `out' post treat post_treat  if `z'==1 & YEAR<2007, absorb(YEAR sp_type) cluster(geo)
			estadd ysumm
			estimates save $out/saved/ceda_tax_dd_`out'_4, replace
// 		exclude CC
		eststo `out'`z'5: reghdfe `out' post treat post_treat  if `z'==1 & sp_type!=3, absorb(YEAR sp_type) cluster(geo)
			estadd ysumm
			estimates save $out/saved/ceda_tax_dd_`out'_5, replace

		*main results weighted
		 eststo `out'`z'1w: reghdfe `out' post treat post_treat  if `z'==1 [aweight=pop_relev], absorb(YEAR) cluster(geo)
		 	estadd ysumm
		 	estimates save $out/saved/ceda_tax_dd_`out'_1w, replace
		**excluding overcrowded
		eststo `out'`z'1oc: reghdfe `out' post treat post_treat  if `z'==1 & (overcrowdedN==0 | sp_type!=1), absorb(YEAR) cluster(geo)
		 	estadd ysumm
		 	estimates save $out/saved/ceda_tax_dd_`out'_1oc, replace

	}
}

*** Figure 3
forvalues y=1995/2016 {
	gen t_`y'=treat*(YEAR==`y')
}
rename t_2000 omityear

*run them
foreach z in ty_go {
	foreach out in sh pass {
		reg `out'  treat t_*  if `z'==1, cluster(geo) 
		mat mat_`out'1=0,0,0
		forvalues g=1995/2016{
			if `g'!=2000 {
				mat a=_b[t_`g'], _se[t_`g'], `g'
				mat mat_`out'1=mat_`out'1\a
			}			
		}
	}
	*spit them out
	foreach reg in 1 {
		foreach out in sh pass {
			preserve
				clear
				svmat mat_`out'`reg'
				rename mat_`out'`reg'1 b
				rename mat_`out'`reg'2 se
				rename mat_`out'`reg'3 t
				replace t=2000 if t==0
				gen ul=b+1.96*se
				gen ll=b-1.96*se
				sort t
				#delimit;
				twoway (scatter b t, connect(l) lpattern(solid) lcolor(black) mcolor(black) msymbol(o))|| 
						(line ul ll t if t<2000, lcolor(black black) lpattern(dash dash))||
						(line ul ll t if t>2000, lcolor(black black) lpattern(dash dash)),
						legend(off) `figbacks' scale(1.96) xtitle("Year") yline(0, lcolor(black)) xline(2000, lcolor(gray));
				#delimit cr
				graph export "$out/saved/ceda_taxbond_`z'_`out'_`reg'.png", replace
			restore
		}
	}
			
}

copy "$out/saved/ceda_taxbond_ty_go_sh_1.png" "$out/Figure3_PanelA.png"
copy "$out/saved/ceda_taxbond_ty_go_pass_1.png" "$out/Figure3_PanelB.png"			


***Figure A1
keep if ty_go & inlist(sp_type,1,3)
drop if inlist(YEAR,2009) //have only 4 and 11 refs respectively
label var sh "Fraction of votes in favor"

local var "sh"
cap drop CDFbyyear
assert !mi(YEAR,`var')
sort YEAR `var'
by YEAR: gen CDFbyyear=_n/_N
label var CDFbyyear "Cumulative density"

local cmd ""
local leglabs ""
local i=1
levelsof YEAR, local(years)
forvalues year=1996/2000 {
disp "`i': `year'"
	local intensity=1-(`i'-1)*(.7/(2000-1996))
	local cmd `"`cmd' (line CDFbyyear `var' if YEAR==`year', lcolor(green*`intensity'))"'
	local leglabs "`leglabs' label(`i' `year')"
	local i=`i' + 1
}
local lablastpre=`i'-1

local cmd `"`cmd' (line CDFbyyear `var' if YEAR==2001, lcolor(yellow))"'
local leglabs "`leglabs' label(`i' 2001)"
local lab2001=`i'

local i=`i'+1
local labfirstpost=`i'

local k=1
forvalues year=2002/2016 {
	disp "`i': `year'"
	local intensity=1-(`k'-1)*(.8/(2016-2002))
	if !inlist(`year',2009) {
		local cmd `"`cmd' (line CDFbyyear `var' if YEAR==`year', lcolor(blue*`intensity'))"'
		local leglabs "`leglabs' label(`i' `year')"
		local i=`i' + 1
		local k=`k'+1
	}

}
local lablastpost=`i'-1
disp `"`cmd'"'
disp "`leglabs'"
disp `"legend(`leglabs' rows(1) order(1 - "--" `lablastpre' `lab2001' `labfirstpost' - "--" `lablastpost')) name(`var'CDFoverlay, replace) `figbacks'"'
twoway `cmd' , ytitle("Cumulative density" " ") scale(1.2) legend(`leglabs' rows(3) order(1 - "--" `lablastpre'  `labfirstpost' - "--" `lablastpost' `lab2001') region(lstyle(none))) name(`var'CDFoverlay, replace) `figbacks'
graph export "$out/FigureA1.png", replace




**********************************************
*Spit everything out into tables
**********************************************
use $data/cedapanel,clear
sum pctnhwhite if YEAR==2000,d
local mednhwhite=r(p50)
gen highminority=(pctnhwhite<`mednhwhite')
gen post_highminority=highminority*post
gen treatpost_highminority=highminority*treat*post
gen post_highfrac=.
gen treatpost_highfrac=.

//conds
foreach out in sh pass {
foreach g in 0a 0b 1 1w 1oc 2 3a 3b 4 5{
estimates use 	 $out/saved/ceda_tax_dd_`out'_`g', 
	eststo tax_`out'_`g'
}
}



*unconds
foreach out in  d_ty_ptax d_pa_ty_ptax d_ty_go d_pa_ty_go d_count d_pa_count d_pa_ty_go dollars pa_dollars {
	foreach g in 4 4a 4b 5 7 7w 7oc 8oc 9 10 10oc{
	estimates use $out/saved/ceda_disle_dd_`out'_`g'
	eststo disle_`out'_`g'
	}
	}
label var post "Post 2001"
label var post_treat "Post x Treat"
label var treat "Treated "	
label var post_highminority " Post x High Minority"
label var treatpost_highminority " Post x Treat x High Minority"
label var post_highfrac " Post x High Fractionalization"
label var treatpost_highfrac " Post x Treat x High Fractionalization"
local theusual "nomtitles nonotes noobs nonumbers nolines nogaps label prehead(" ") posthead(" ") prefoot(" ") postfoot(" ")"
	
***Table 3: Effect of Proposition 39 on Jurisdiction-Level Proposal Behavior
esttab  disle_d_ty_go_4 disle_d_ty_go_7  disle_dollars_4 disle_dollars_7 using $out/Table3.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace
		
***Table 4: Effect of Proposition 39 on Election-Level Results
esttab tax_sh_1 tax_sh_2    tax_pass_1 tax_pass_2    using $out/Table4.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace

***Table 5: Effect of Proposition 39 on Jurisdiction-Level Funding Outcomes
esttab disle_d_pa_ty_go_4 disle_d_pa_ty_go_7  disle_pa_dollars_4 disle_pa_dollars_7 using $out/Table5.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace

***Appendix Table A5 - Effect of Proposition 39 on Jurisdiction-Level Tax Outcomes
esttab  disle_d_ty_ptax_7  disle_d_pa_ty_ptax_7 disle_d_count_7 disle_d_pa_count_7  using $out/TableA5.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace
	
***Appendix Table A2
esttab  disle_d_ty_go_9 disle_d_ty_go_10 disle_dollars_9 disle_dollars_10 disle_d_pa_ty_go_9 disle_d_pa_ty_go_10 disle_pa_dollars_9 disle_pa_dollars_10 using $out/TableA2.tex, keep(post_treat  post_highfrac treatpost_highfrac) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace

***Appendix Table A4
esttab disle_d_ty_go_4a disle_dollars_4a tax_sh_4 tax_pass_4 disle_d_pa_ty_go_4a  disle_pa_dollars_4a using $out/TableA4.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace
		
*Appendix Table A3: Excluding CC's
esttab disle_d_ty_go_5 disle_dollars_5 tax_sh_5 tax_pass_5 disle_d_pa_ty_go_5 disle_pa_dollars_5 using $out/TableA3.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace

***Appendix Table A1
esttab  disle_d_ty_go_4b disle_dollars_4b tax_sh_3a  tax_sh_3b  tax_pass_3a tax_pass_3b disle_d_pa_ty_go_4b  disle_pa_dollars_4b using $out/TableA1.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace
***Appendix Table A7: Main Effects, Weighting by Jurisdiction Population
esttab  disle_d_ty_go_7w disle_dollars_7w tax_sh_1w  tax_pass_1w disle_d_pa_ty_go_7w  disle_pa_dollars_7w using $out/TableA7.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace
*** Appendix Table A6 - Effect on Election-Level Results, Including All Tax Measures
esttab tax_sh_0a tax_sh_0b tax_pass_0a tax_pass_0b   using $out/TableA6.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace
*** Appendix Table A8 Main Effects, Excluding Critically Overcrowded Schools
esttab  disle_d_ty_go_7oc disle_dollars_7oc tax_sh_1oc  tax_pass_1oc disle_d_pa_ty_go_7oc  disle_pa_dollars_7oc using $out/TableA8.tex, keep(post_treat) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace
*** Appendix Table A9 Heterogeneity, Excluding Critically Overcrowded Schools
esttab  disle_dollars_8oc disle_pa_dollars_8oc disle_dollars_10oc disle_pa_dollars_10oc using $out/TableA9.tex, keep(post_treat  post_highminority treatpost_highminority post_highfrac treatpost_highfrac) stat(ymean N r2, label("Y-Mean" "N" "R-sq")) se `theusual' replace